package com.atguigu.gulimall.order.listener;

import com.atguigu.common.to.mq.SeckillOrderTo;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "order.seckill.order.queue")
@Service
public class SeckillOrderListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void handleSeckillOrder(Message message, SeckillOrderTo seckillOrderTo, Channel channel) throws IOException {
        System.out.println("收到秒杀订单,准备处理");
        try {
            orderService.handleSeckillOrder(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            throw new RuntimeException(e);
        }
    }

}
